<template>
  <div class="cool-pagination">
    <div class="pagination-container">
      <div class="pagination-info" v-if="showTotal">
        <span class="total-text"
          >共 <span class="total-count">{{ total }}</span> 条</span
        >
      </div>

      <div class="pagination-controls">
        <div
          class="pagination-btn prev-btn"
          :class="{ disabled: currentPage <= 1 }"
          @click="handlePrev"
        >
          <span class="iconfont icon-shangyiyedanjiantou"></span>
        </div>

        <div class="pagination-pages">
          <div
            v-if="showFirstPage || true"
            class="page-item"
            :class="{ active: currentPage === 1 }"
            @click="handlePageChange(1)"
          >
            1
          </div>

          <div
            v-if="showPrevMore"
            class="page-item more left-more"
            @mouseenter="leftMoreVisible = true"
            @mouseleave="leftMoreVisible = false"
            @click="handlePrevMoreClick"
          >
            <span v-if="!leftMoreVisible" class="more-icon">...</span>
            <span
              v-else
              class="more-icon-active iconfont icon-shangyiye"
            ></span>
          </div>

          <div
            v-for="page in pagerPages"
            :key="page"
            class="page-item"
            :class="{ active: currentPage === page }"
            @click="handlePageChange(page)"
          >
            {{ page }}
          </div>

          <div
            v-if="showNextMore"
            class="page-item more right-more"
            @mouseenter="rightMoreVisible = true"
            @mouseleave="rightMoreVisible = false"
            @click="handleNextMoreClick"
          >
            <span v-if="!rightMoreVisible" class="more-icon">...</span>
            <span v-else class="more-icon-active iconfont icon-xiayiye"></span>
          </div>

          <div
            v-if="showLastPage"
            class="page-item"
            :class="{ active: currentPage === pageCount }"
            @click="handlePageChange(pageCount)"
          >
            {{ pageCount }}
          </div>
        </div>

        <div
          class="pagination-btn next-btn"
          :class="{ disabled: currentPage >= pageCount }"
          @click="handleNext"
        >
          <span class="iconfont icon-xiayiyedanjiantou"></span>
        </div>
      </div>

      <div class="pagination-jumper" v-if="showJumper">
        <span class="jumper-text">前往</span>
        <div class="jumper-input-wrapper">
          <input
            type="number"
            class="jumper-input"
            v-model.number="jumpPage"
            @keyup.enter="handleJump"
            min="1"
            :max="pageCount"
          />
        </div>
        <span class="jumper-text">页</span>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "CoolPagination",
  props: {
    total: {
      type: Number,
      default: 0,
    },
    currentPage: {
      type: Number,
      default: 1,
    },
    pageSize: {
      type: Number,
      default: 10,
    },
    pagerCount: {
      type: Number,
      default: 7,
    },
    showTotal: {
      type: Boolean,
      default: true,
    },
    showJumper: {
      type: Boolean,
      default: true,
    },
  },
  data () {
    return {
      jumpPage: this.currentPage,
      leftMoreVisible: false,
      rightMoreVisible: false,
    };
  },
  computed: {
    pageCount () {
      return Math.max(1, Math.ceil(this.total / this.pageSize));
    },
    pagerPages () {
      const pagerCount = this.pagerCount;
      const halfPagerCount = Math.floor(pagerCount / 2);
      const currentPage = this.currentPage;
      const pageCount = this.pageCount;

      let showPagerPages = [];

      if (pageCount <= pagerCount) {
        for (let i = 2; i < pageCount; i++) {
          showPagerPages.push(i);
        }
      } else {
        if (currentPage <= halfPagerCount) {
          for (let i = 2; i <= pagerCount - 2; i++) {
            showPagerPages.push(i);
          }
        } else if (currentPage >= pageCount - halfPagerCount) {
          for (let i = pageCount - pagerCount + 3; i <= pageCount - 1; i++) {
            showPagerPages.push(i);
          }
        } else {
          for (
            let i = currentPage - Math.ceil(pagerCount / 4);
            i <= currentPage + Math.floor(pagerCount / 4);
            i++
          ) {
            showPagerPages.push(i);
          }
        }
      }

      return showPagerPages;
    },
    showFirstPage () {
      return this.pageCount > 1;
    },
    showLastPage () {
      return (
        this.pageCount > 1 &&
        this.pagerPages[this.pagerPages.length - 1] !== this.pageCount
      );
    },
    showPrevMore () {
      return this.pagerPages.length > 0 && this.pagerPages[0] > 2;
    },
    showNextMore () {
      return (
        this.pagerPages.length > 0 &&
        this.pagerPages[this.pagerPages.length - 1] < this.pageCount - 1
      );
    },
  },
  watch: {
    currentPage: {
      immediate: true,
      handler (val) {
        this.jumpPage = val;
      },
    },
  },
  methods: {
    handlePageChange (page) {
      if (page !== this.currentPage) {
        this.$emit("update:currentPage", page);
        this.$emit("current-change", page);
      }
    },
    handlePrev () {
      if (this.currentPage > 1) {
        this.handlePageChange(this.currentPage - 1);
      }
    },
    handleNext () {
      if (this.currentPage < this.pageCount) {
        this.handlePageChange(this.currentPage + 1);
      }
    },
    handleJump () {
      let page = Math.floor(this.jumpPage);
      if (isNaN(page) || page < 1) {
        page = 1;
      }
      if (page > this.pageCount) {
        page = this.pageCount;
      }
      this.handlePageChange(page);
    },
    handlePrevMoreClick () {
      this.handlePageChange(this.currentPage - 5);
    },
    handleNextMoreClick () {
      this.handlePageChange(this.currentPage + 5);
    },
  },
};
</script>

<style scoped lang="less">
.cool-pagination {
  display: flex;
  justify-content: center;
  margin-top: 20px 0;
  font-family: "PingFang SC", -apple-system, BlinkMacSystemFont, "Segoe UI",
    "Helvetica Neue", Arial, sans-serif;

  .pagination-container {
    display: flex;
    align-items: center;
    background: rgba(255, 255, 255, 0.1);
    backdrop-filter: blur(5px);
    border-radius: 30px;
    padding: 6px 15px;
    box-shadow: 0 5px 15px rgba(75, 88, 220, 0.15);
    transition: all 0.3s ease;

    &:hover {
      box-shadow: 0 8px 20px rgba(75, 88, 220, 0.2);
    }

    .pagination-info {
      margin-right: 15px;

      .total-text {
        color: #606266;
        font-size: 13px;

        .total-count {
          color: #4b58dc;
          font-weight: bold;
        }
      }
    }

    .pagination-controls {
      display: flex;
      align-items: center;

      .pagination-btn {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 32px;
        height: 32px;
        margin: 0 3px;
        cursor: pointer;
        background: linear-gradient(135deg, #4b58dc, #6f7dff);
        color: white;
        border-radius: 50%;
        font-size: 12px;
        transition: all 0.3s ease;
        box-shadow: 0 3px 8px rgba(75, 88, 220, 0.3);
        position: relative;
        overflow: hidden;

        &:before {
          content: "";
          position: absolute;
          top: 0;
          left: -100%;
          width: 100%;
          height: 100%;
          background: linear-gradient(
            90deg,
            transparent,
            rgba(255, 255, 255, 0.3),
            transparent
          );
          transition: all 0.4s ease;
        }

        &:hover {
          transform: translateY(-2px);
          box-shadow: 0 5px 12px rgba(75, 88, 220, 0.4);

          &:before {
            left: 100%;
          }
        }

        &:active {
          transform: translateY(0);
          box-shadow: 0 2px 5px rgba(75, 88, 220, 0.3);
        }

        &.disabled {
          background: linear-gradient(135deg, #aab2f2, #b9c0ff);
          opacity: 0.6;
          cursor: not-allowed;
          box-shadow: none;

          &:hover {
            transform: none;
            box-shadow: none;
          }

          &:before {
            display: none;
          }
        }
      }

      .pagination-pages {
        display: flex;
        align-items: center;
        margin: 0 5px;

        .page-item {
          display: flex;
          align-items: center;
          justify-content: center;
          min-width: 32px;
          height: 32px;
          margin: 0 3px;
          padding: 0 5px;
          cursor: pointer;
          background: white;
          color: #606266;
          border-radius: 6px;
          font-size: 13px;
          font-weight: 500;
          transition: all 0.3s ease;
          box-shadow: 0 2px 5px rgba(0, 0, 0, 0.05);
          user-select: none;

          &:hover:not(.active) {
            color: #4b58dc;
            background: #f5f7ff;
            transform: translateY(-2px);
            box-shadow: 0 4px 10px rgba(75, 88, 220, 0.15);
          }

          &.active {
            background: linear-gradient(135deg, #4b58dc, #6f7dff);
            color: white;
            box-shadow: 0 3px 8px rgba(75, 88, 220, 0.3);
            transform: translateY(-1px);
          }

          &.more {
            color: #606266;
            font-weight: bold;

            .more-icon-active {
              color: #4b58dc;
              font-size: 12px;
            }
          }
        }
      }
    }

    .pagination-jumper {
      display: flex;
      align-items: center;
      margin-left: 15px;

      .jumper-text {
        color: #606266;
        font-size: 13px;
      }

      .jumper-input-wrapper {
        margin: 0 5px;
        background: white;
        border-radius: 4px;
        padding: 2px 0;
        width: 50px;
        height: 26px;
        display: flex;
        align-items: center;
        justify-content: center;
        box-shadow: 0 2px 5px rgba(0, 0, 0, 0.05);
        transition: all 0.3s ease;

        &:focus-within {
          box-shadow: 0 0 0 2px rgba(75, 88, 220, 0.2);
        }

        .jumper-input {
          width: 100%;
          height: 100%;
          text-align: center;
          border: none;
          outline: none;
          color: #606266;
          font-size: 14px;
          padding: 0;

          /* Hide number input arrows */
          &::-webkit-outer-spin-button,
          &::-webkit-inner-spin-button {
            -webkit-appearance: none;
            margin: 0;
          }
          &[type="number"] {
            -moz-appearance: textfield;
          }
        }
      }
    }
  }
}

/* Keyframe animations for loading effects */
@keyframes pulse {
  0% {
    transform: scale(0.95);
    box-shadow: 0 0 0 0 rgba(75, 88, 220, 0.4);
  }
  70% {
    transform: scale(1);
    box-shadow: 0 0 0 8px rgba(75, 88, 220, 0);
  }
  100% {
    transform: scale(0.95);
    box-shadow: 0 0 0 0 rgba(75, 88, 220, 0);
  }
}
</style>
